﻿package com.athena.process
{
	import flash.display.MovieClip;
	import flash.display.*;
	import flash.text.TextFormat;
	import flash.utils.*;
	import flash.events.*;
	import fl.containers.ScrollPane; 
	import fl.controls.ScrollPolicy; 
	import com.adobe.serialization.json.JSON;
	import com.athena.controller.MailItemController;
	/**
	 * ...
	 * @author ...
	 */
	public class TopFunction 
	{
		public static var arrCT_Trongngay : Array = new Array() ;
		public static var arrCT_Homtruoc : Array = new Array() ;
		public static var arrCT_Tuan : Array = new Array() ;
		public static var arrCT_Thang : Array = new Array() ;
		public static var arrDG : Array = new Array() ;		
		/********************************** Cao thu Dai gia Tin tuc **************************************************/
		public static function CheckExistTop25(name : String, arr : Array) : Boolean {
			for (var i:int = 0; i < arr.length; i++) 
			{
				if (arr[i].N == name) return true ;				
			}
			return false ;
		}
		public static function DisplayTopCT_Trongngay(arr : Array, type : int) : void {
			if (arr == null) return ;
			if (arr.length == 0) return ;
			if (Cocaro.mainStage_mc.currentFrame != 3) return ;
			//tf.appendText("Cao thu trong ngay");
			//if (Cocaro.mainStage_mc.currentFrame == 3) {
				//for (var j:int = 0; j < 10; j++) 
				//{
					//Cocaro.mainStage_mc.CT_mc.CTTrongngay_mc["name" + j.toString() + "_txt"].text = "" ;
					//Cocaro.mainStage_mc.CT_mc.CTTrongngay_mc["Result" + j.toString() + "_txt"].text = "";
				//}
			//}
			//if (arr == null) return ;
			//tf.appendText("Length:" + arr.length) ;
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 12 ;
			var formatTextTop : TextFormat = new TextFormat() ;				
			formatTextTop.font = "Tahoma" ;
			formatTextTop.bold = true ;
			formatTextTop.size = 14 ;
			//{Hien thi giao dien
			for (var j:int = 0; j < arr.length; j++) 
			{
				if (j > 9) break ;
				var item = arr[j];
				Cocaro.mainStage_mc.CT_mc.CTTrongngay_mc["name" + j.toString() + "_txt"].text = item.N ;
				Cocaro.mainStage_mc.CT_mc.CTTrongngay_mc["Result" + j.toString() + "_txt"].text = BasicFunction.numberFormat(item.A, 0, false, true) + " AG" ;
				//if (type == 0)
					//Cocaro.mainStage_mc.CT_mc.CTTrongngay_mc["Result" + j.toString() + "_txt"].text = Cocaro.mainStage_mc.CT_mc.CTTrongngay_mc["Result" + j.toString() + "_txt"].text + " AG" ;
				if ( j == 0 )
				{
					Cocaro.mainStage_mc.CT_mc.CTTrongngay_mc["name" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
					Cocaro.mainStage_mc.CT_mc.CTTrongngay_mc["Result" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
				} else{
					Cocaro.mainStage_mc.CT_mc.CTTrongngay_mc["name" + j.toString() + "_txt"].setTextFormat(formatText) ;
					Cocaro.mainStage_mc.CT_mc.CTTrongngay_mc["Result" + j.toString() + "_txt"].setTextFormat(formatText) ;
				}
				
			}
			//}
		}
		public static function DisplayTopCT_Homtruoc(arr : Array) : void {
			if (arr == null) return ;
			if (arr.length == 0) return ;
			if (Cocaro.mainStage_mc.currentFrame != 3) return ;
			var Yesterday:Date = new Date();			
			Yesterday.setDate(Yesterday.getDate() - 1);
			var day1: String = Yesterday.getDate().toString();
			var month1 = (Yesterday.getMonth() + 1).toString()
			Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc.Title_txt.text = "Top Cao thủ Ba cây ngày " + day1 + "/" + month1;  //Yesterday.toLocaleDateString(); //Yesterday.getDate().toString() + "/" (Yesterday.getMonth() + 1).toString;//(today - 1).toString() + "/" + (month+ 1).toString();
			//if (Cocaro.mainStage_mc.currentFrame == 3) {
				//for (var j:int = 0; j < 10; j++) 
				//{
					//Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc["name" + j.toString() + "_txt"].text = "" ;
					//Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc["Result" + j.toString() + "_txt"].text = ""; 
					//Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc["Mark" + j.toString() + "_txt"].text = "" ;
				//}
			//}
			//if (arr == null) return ;
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 12 ;
			var formatTextTop : TextFormat = new TextFormat() ;				
			formatTextTop.font = "Tahoma" ;
			formatTextTop.bold = true ;
			formatTextTop.size = 14 ;
			//{Hien thi giao dien
			for (var j:int = 0; j < arr.length; j++) 
			{
				if (j > 9) break ;
				var item = arr[j];
				Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc["name" + j.toString() + "_txt"].text = item.N ;
				Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc["Result" + j.toString() + "_txt"].text = BasicFunction.numberFormat(item.A, 0, false, true)  + " AG"; 
				Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc["Mark" + j.toString() + "_txt"].text = item.M ;
				if ( j == 0)
				{
					Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc["name" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
					Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc["Result" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
					Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc["Mark" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
				}
				else {
					Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc["name" + j.toString() + "_txt"].setTextFormat(formatText) ;
					Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc["Result" + j.toString() + "_txt"].setTextFormat(formatText) ;
					Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc["Mark" + j.toString() + "_txt"].setTextFormat(formatText) ;
				}
			}
			//}
		}
		public static function DisplayTopCT_Tuan(arr : Array) : void { 
			if (arr == null) return ;
			if (arr.length == 0) return ;
			if (Cocaro.mainStage_mc.currentFrame != 3) return ;
			//Cocaro.tf.appendText("=>1") ;
			//tf.appendText("\n Nhay vao ct tuan")
			//if (Cocaro.mainStage_mc.currentFrame == 3) {
				//for (var j:int = 0; j < 10; j++) 
				//{
					//Cocaro.mainStage_mc.CT_mc.CTTuanThang_mc["name" + j.toString() + "_txt"].text = "" ;
					//Cocaro.mainStage_mc.CT_mc.CTTuanThang_mc["Result" + j.toString() + "_txt"].text = ""; 
					//Cocaro.mainStage_mc.CT_mc.CTTuanThang_mc["Mark" + j.toString() + "_txt"].text = "" ;
				//}
			//}
			//if (arr == null) return ;
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 12 ;
			var formatTextTop : TextFormat = new TextFormat() ;				
			formatTextTop.font = "Tahoma" ;
			formatTextTop.bold = true ;
			formatTextTop.size = 14 ;
			//Cocaro.tf.appendText("=>Len:" + arr.length) ;
			//{Hien thi giao dien
			for (var j:int = 0; j < arr.length; j++) 
			{
				if (j > 9) break ;
				var item = arr[j];
				Cocaro.mainStage_mc.CT_mc.CTTuan_mc["name" + j.toString() + "_txt"].text = item.N ;
				Cocaro.mainStage_mc.CT_mc.CTTuan_mc["Mark" + j.toString() + "_txt"].text = item.A ;					
				if ( j == 0)
				{
					Cocaro.mainStage_mc.CT_mc.CTTuan_mc["name" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
					Cocaro.mainStage_mc.CT_mc.CTTuan_mc["Mark" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
				} else {
					Cocaro.mainStage_mc.CT_mc.CTTuan_mc["name" + j.toString() + "_txt"].setTextFormat(formatText) ;
					Cocaro.mainStage_mc.CT_mc.CTTuan_mc["Mark" + j.toString() + "_txt"].setTextFormat(formatText) ;
				}
			}
			//}
		}
		public static function DisplayTopCT_Thang(arr : Array) : void {
			if (arr == null) return ;
			if (arr.length == 0) return ;
			if (Cocaro.mainStage_mc.currentFrame != 3) return ;
			//if (Cocaro.mainStage_mc.currentFrame == 3) {
				//for (var j:int = 0; j < 10; j++) 
				//{
					//Cocaro.mainStage_mc.CT_mc.CTThang_mc["name" + j.toString() + "_txt"].text = "" ;
					//Cocaro.mainStage_mc.CT_mc.CTThang_mc["Result" + j.toString() + "_txt"].text = ""; 
					//Cocaro.mainStage_mc.CT_mc.CTThang_mc["Mark" + j.toString() + "_txt"].text = "" ;
				//}
			//}
			//if (arr == null) return ;
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 12 ;
			var formatTextTop : TextFormat = new TextFormat() ;				
			formatTextTop.font = "Tahoma" ;
			formatTextTop.bold = true ;
			formatTextTop.size = 14 ;
			//{Hien thi giao dien
			var LastMonth: Date = new Date();
			var month = (LastMonth.getMonth() +1).toString();
			Cocaro.mainStage_mc.CT_mc.CTThang_mc.Title_txt.text = "Top Cao thủ Ba cây tháng " + month;
			for (var j:int = 0; j < arr.length; j++) 
			{
				if (j > 9) break ;
				var item = arr[j];
				Cocaro.mainStage_mc.CT_mc.CTThang_mc["name" + j.toString() + "_txt"].text = item.N ;
				Cocaro.mainStage_mc.CT_mc.CTThang_mc["Mark" + j.toString() + "_txt"].text = item.A ;				
				if ( j == 0)
				{
					Cocaro.mainStage_mc.CT_mc.CTThang_mc["name" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
					Cocaro.mainStage_mc.CT_mc.CTThang_mc["Mark" + j.toString() + "_txt"].setTextFormat(formatTextTop) ;
				}
				else {
					Cocaro.mainStage_mc.CT_mc.CTThang_mc["name" + j.toString() + "_txt"].setTextFormat(formatText) ;
					Cocaro.mainStage_mc.CT_mc.CTThang_mc["Mark" + j.toString() + "_txt"].setTextFormat(formatText) ;
				}
			}
			//}
		}
		public static function DisplayTopDG(arr : Array) : void { 
			if (arr == null) return ;
			if (arr.length == 0) return ;
			if (Cocaro.mainStage_mc.currentFrame != 3) return ;
			//if (Cocaro.mainStage_mc.currentFrame == 3) {
				//for (var j:int = 0; j < 10; j++) 
				//{
					//Cocaro.mainStage_mc.DG_mc["name" + j.toString() + "_txt"].text = "" ;
					//Cocaro.mainStage_mc.DG_mc["Result" + j.toString() + "_txt"].text = ""; 
				//}
			//}			
			//if (arr == null) return ;
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.size = 12 ;
			////Cocaro.tf.appendText("=>DisplayDG:" + arr.length) ;
			//{Hien thi giao dien
			for (var j:int = 0; j < arr.length; j++) 
			{
				if (j > 9) break ;
				var item = arr[j];
				Cocaro.mainStage_mc.DG_mc["name" + j.toString() + "_txt"].text = item.N ;
				Cocaro.mainStage_mc.DG_mc["Result" + j.toString() + "_txt"].text = BasicFunction.numberFormat(item.A, 0, false, true) + " AG" ;; 
				Cocaro.mainStage_mc.DG_mc["name" + j.toString() + "_txt"].setTextFormat(formatText) ;
				Cocaro.mainStage_mc.DG_mc["Result" + j.toString() + "_txt"].setTextFormat(formatText) ;
			}
			//}
		}
		
		/* Nhan du lieu va day vao Mang de luu giu */
		public static function TopCT_Trongngay(str : String) : void {
			arrCT_Trongngay = new Array() ;
			var arr : Array = (JSON.decode(str) as Array) ;
			for (var i:int = 0; i < arr.length; i++) 
			{
				//Check xem chua ton tai trong Danh sach chua ==> Add vao danh sach
				if (!CheckExistTop25(arr[i].N, arrCT_Trongngay)) {
					var obj : Object = new Object() ;
					obj.N = arr[i].N ;
					obj.A = arr[i].A ;
					arrCT_Trongngay.push(obj) ;
				}
				//if (i == arr.length -1)
				//	DisplayTopCT_Trongngay(arrCT_Trongngay, 0) ;
			}			
		}
		public static function TopCT_Homtruoc(str : String) : void {
			//tf.appendText("\n get top hom truoc");
			arrCT_Homtruoc = new Array() ;
			var arr : Array = (JSON.decode(str) as Array) ;
			for (var i:int = 0; i < arr.length; i++) 
			{
				//Check xem chua ton tai trong Danh sach chua ==> Add vao danh sach
				if (!CheckExistTop25(arr[i].N, arrCT_Homtruoc)) {
					var obj : Object = new Object() ;
					obj.N = arr[i].N ;
					obj.A = arr[i].A ;
					obj.M = arr[i].M ;
					arrCT_Homtruoc.push(obj) ;
				}
				//if (i == arr.length -1)
				//	DisplayTopCT_Homtruoc(arrCT_Homtruoc) ;
			}
		}
		public static function TopCT_Tuan(str : String) : void {
			//Cocaro.tf.appendText("=>CTT:" + str) ;
			arrCT_Tuan = new Array() ;
			var arr : Array = (JSON.decode(str) as Array) ;
			for (var i:int = 0; i < arr.length; i++) 
			{
				//Check xem chua ton tai trong Danh sach chua ==> Add vao danh sach
				if (!CheckExistTop25(arr[i].N, arrCT_Tuan)) {
					var obj : Object = new Object() ;
					obj.N = arr[i].N ;
					obj.A = arr[i].A ;
					//obj.M = arr[i].M ;
					arrCT_Tuan.push(obj) ;
				}
			}
		}
		public static function TopCT_Thang(str : String) : void {
			arrCT_Thang = new Array() ;
			var arr : Array = (JSON.decode(str) as Array) ;
			for (var i:int = 0; i < arr.length; i++) 
			{
				//Check xem chua ton tai trong Danh sach chua ==> Add vao danh sach
				if (!CheckExistTop25(arr[i].N, arrCT_Thang)) {
					var obj : Object = new Object() ;
					obj.N = arr[i].N ;
					obj.A = arr[i].A ;
					//obj.M = arr[i].M ;
					arrCT_Thang.push(obj) ;
				}
				//var dtCheck : Date = new Date() ;
				//if (dtCheck.date == 1) {
					//if (i == arr.length -1)
						//DisplayTopCT_Thang(arrCT_Thang) ;
				//}
			}
		}
		public static function TopDG(str : String) : void {
			arrDG = new Array() ;
			var arr : Array = (JSON.decode(str) as Array) ;
			////Cocaro.tf.appendText("==>PushDG:" + arr.length) ;
			for (var i:int = 0; i < arr.length; i++) 
			{
				//Check xem chua ton tai trong Danh sach chua ==> Add vao danh sach
				if (!CheckExistTop25(arr[i].N, arrDG)) {
					////Cocaro.tf.appendText("==>PushDG1:" + arr[i].N + "-" + arr[i].A) ;
					var obj : Object = new Object() ;
					obj.N = arr[i].N ;
					obj.A = arr[i].A ;
					arrDG.push(obj) ;
				}
				//if (i == arr.length -1)
					//DisplayTopDG(arrDG) ;
			}			
		}
		
		public static function DisplayHotNew() : void {
			if (Cocaro.myXML == null) return ;
			var strMain : String = "	" ;	
			for each (var hotNew:XML in Cocaro.myXML.children()){
				if (hotNew.name().toString() == "HOTNEWS") {
					for (var i:int = 0; i<hotNew.*.length(); i++){
						var str : String = "" ;
						if (hotNew.CONTENT[i].Text[0] == "BR") {
							str = "<br/>" ;
							str = str + "	" ;
						} else if (hotNew.CONTENT[i].Text[0] == "SPACE") {
							str = str + "	" ;
						} else {
							str = "<font color=\"" + hotNew.CONTENT[i].Color[0] + "\" face=\"" 
									+ hotNew.CONTENT[i].Font[0] + "\" Size=\"" + hotNew.CONTENT[i].Size[0] + "\">" ;
							if (hotNew.CONTENT[i].Bold[0] == 1) str = str + "<b>" ;
							str = str + hotNew.CONTENT[i].Text[0] ;
							if (hotNew.CONTENT[i].Bold[0] == 1) str = str + "</b>" ;
							str = str + "</font>" ;
						}
						strMain = strMain + " " + str ;
					}
					Cocaro.mainStage_mc.New_txt.Content_txt.htmlText = strMain ;	
					break ;
				} else if (hotNew.name().toString() == "TABS"){
					Cocaro.tabDefault = hotNew.stt;
				}
			}
		}
		
		public static function DisplayNew() : void {
			if (Cocaro.myXML == null) return ;
			var strMain : String = "		" ;	
			var strMain1 : String = "		" ;	
			for each (var hotNew:XML in Cocaro.myXML.children()){
				if (hotNew.name().toString() == "TABS") {    // hien thi khung tin tuc khi can
					//tabShow = hotNew.stt;					
				} else if (hotNew.name().toString() == "NEWS") {
					var formatText : TextFormat = new TextFormat() ;				
					formatText.font = "Times New Roman" ;
					formatText.bold = true ;
					formatText.italic = true ;
					formatText.size = 20 ;
					Cocaro.mainStage_mc.New_mc.Title_txt.text = hotNew.Title[0] ;
					Cocaro.mainStage_mc.New_mc.Title_txt.setTextFormat(formatText) ;
					var formatText1 : TextFormat = new TextFormat() ;				
					formatText1.font = "Tahoma" ;
					formatText1.bold = true ;			
					formatText1.size = 13 ;
					for each(var person:XML in hotNew.children()) {
						if (person.name().toString() == "Persons") {
							for (var i:int = 0; i < person.*.length(); i++) {
								if (i > 9) break ;
								Cocaro.mainStage_mc.New_mc["Name" + i.toString() + "_txt"].text = person.Person[i].Name[0] ;
								Cocaro.mainStage_mc.New_mc["Mark" + i.toString() + "_txt"].text = person.Person[i].Mark[0] ;
								Cocaro.mainStage_mc.New_mc["Money" + i.toString() + "_txt"].text = person.Person[i].Money[0] ;
								Cocaro.mainStage_mc.New_mc["Name" + i.toString() + "_txt"].setTextFormat(formatText1) ;
								Cocaro.mainStage_mc.New_mc["Mark" + i.toString() + "_txt"].setTextFormat(formatText1) ;
								Cocaro.mainStage_mc.New_mc["Money" + i.toString() + "_txt"].setTextFormat(formatText1) ;
							}		
						}
					}
				}
			}
		}
	}
	
}